package com.jeegit.ecms.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.jeegit.common.annotation.Log;
import com.jeegit.common.enums.BusinessType;
import com.jeegit.ecms.domain.CmsLinkCategory;
import com.jeegit.ecms.service.ICmsLinkCategoryService;
import com.jeegit.common.core.controller.BaseController;
import com.jeegit.common.core.domain.AjaxResult;
import com.jeegit.common.utils.poi.ExcelUtil;
import com.jeegit.common.utils.StringUtils;
import com.jeegit.common.core.domain.Ztree;

/**
 * 网站分类Controller
 * 
 * @author jeegit
 * @date 2022-06-25
 */
@Controller
@RequestMapping("/ecms/linkcategory")
public class CmsLinkCategoryController extends BaseController
{
    private String prefix = "ecms/linkcategory";

    @Autowired
    private ICmsLinkCategoryService cmsLinkCategoryService;

    @RequiresPermissions("ecms:linkcategory:view")
    @GetMapping()
    public String linkcategory()
    {
        return prefix + "/linkcategory";
    }

    /**
     * 查询网站分类树列表
     */
    @RequiresPermissions("ecms:linkcategory:list")
    @PostMapping("/list")
    @ResponseBody
    public List<CmsLinkCategory> list(CmsLinkCategory cmsLinkCategory)
    {
        List<CmsLinkCategory> list = cmsLinkCategoryService.selectCmsLinkCategoryList(cmsLinkCategory);
        return list;
    }

    /**
     * 导出网站分类列表
     */
    @RequiresPermissions("ecms:linkcategory:export")
    @Log(title = "网站分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(CmsLinkCategory cmsLinkCategory)
    {
        List<CmsLinkCategory> list = cmsLinkCategoryService.selectCmsLinkCategoryList(cmsLinkCategory);
        ExcelUtil<CmsLinkCategory> util = new ExcelUtil<CmsLinkCategory>(CmsLinkCategory.class);
        return util.exportExcel(list, "网站分类数据");
    }

    /**
     * 新增网站分类
     */
    @GetMapping(value = { "/add/{id}", "/add/" })
    public String add(@PathVariable(value = "id", required = false) Long id, ModelMap mmap)
    {
        if (StringUtils.isNotNull(id))
        {
            mmap.put("cmsLinkCategory", cmsLinkCategoryService.selectCmsLinkCategoryById(id));
        }
        return prefix + "/add";
    }

    /**
     * 新增保存网站分类
     */
    @RequiresPermissions("ecms:linkcategory:add")
    @Log(title = "网站分类", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(CmsLinkCategory cmsLinkCategory)
    {
        return toAjax(cmsLinkCategoryService.insertCmsLinkCategory(cmsLinkCategory));
    }

    /**
     * 修改网站分类
     */
    @RequiresPermissions("ecms:linkcategory:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        CmsLinkCategory cmsLinkCategory = cmsLinkCategoryService.selectCmsLinkCategoryById(id);
        mmap.put("cmsLinkCategory", cmsLinkCategory);
        return prefix + "/edit";
    }

    /**
     * 修改保存网站分类
     */
    @RequiresPermissions("ecms:linkcategory:edit")
    @Log(title = "网站分类", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(CmsLinkCategory cmsLinkCategory)
    {
        return toAjax(cmsLinkCategoryService.updateCmsLinkCategory(cmsLinkCategory));
    }

    /**
     * 删除
     */
    @RequiresPermissions("ecms:linkcategory:remove")
    @Log(title = "网站分类", businessType = BusinessType.DELETE)
    @GetMapping("/remove/{id}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("id") Long id)
    {
        return toAjax(cmsLinkCategoryService.deleteCmsLinkCategoryById(id));
    }

    /**
     * 选择网站分类树
     */
    @GetMapping(value = { "/selectLinkcategoryTree/{id}", "/selectLinkcategoryTree/" })
    public String selectLinkcategoryTree(@PathVariable(value = "id", required = false) Long id, ModelMap mmap)
    {
        if (StringUtils.isNotNull(id))
        {
            mmap.put("cmsLinkCategory", cmsLinkCategoryService.selectCmsLinkCategoryById(id));
        }
        return prefix + "/tree";
    }

    /**
     * 加载网站分类树列表
     */
    @GetMapping("/treeData")
    @ResponseBody
    public List<Ztree> treeData()
    {
        List<Ztree> ztrees = cmsLinkCategoryService.selectCmsLinkCategoryTree();
        return ztrees;
    }
}
